(function(){
	var main = {
		username:"muhawo",
		start: function(){
			var tr2 = document.querySelectorAll("#ajaxtable .tr2")[1];
			this.buildToolbar(tr2.children[tr2.children.length-1], "top");
			var tr = tr2.nextElementSibling?tr2.nextElementSibling:{};
			this.cbx = this.buildCheckbox();
			do{
				if(tr.children.length == 5) {
					var cbx = this.getCbx();
					cbx.detailUrl = tr.children[0].children[0].href;
					tr.children[4].appendChild(cbx);	
				}else{
					tr.children[tr.children.length-1].setAttribute("colspan",6);
					tr.className += " tr2";
					this.buildToolbar(tr.children[tr.children.length-1], "bottom");
				}
			}while(tr = tr.nextElementSibling)

			this.addCss();

		},
		getCbx:function(){
			var that = this;
			var newCbx = this.cbx.cloneNode(true);
			newCbx.querySelector("input").addEventListener("click", function(event){
				var target = event.target;
				if(!target.checked){
					var inputs = document.querySelectorAll(".clman-cbx-toolbar input");
					for(var i = 0; i < inputs.length; i++){
						inputs[i].checked = false;
					}
				}
			})
			newCbx.querySelector(".clman-btn").addEventListener("click", function(event){
				var url = event.target.parentNode.detailUrl;
				console.log(url);
				that.download(url);
			})
			return newCbx;
		},
		buildCheckbox: function (){
			var that = this;
			var cbxwrapper = document.createElement("div");
			cbxwrapper.className = "clman-cbx-wrapper";

			var span = document.createElement("span");
			span.textContent = "下载";
			span.className = "clman-btn";
			cbxwrapper.appendChild(span);

			var spliter = document.createElement("span");
			spliter.className = "clman-spliter";
			spliter.textContent = "|";
			cbxwrapper.appendChild(spliter);

			var span = document.createElement("span");
			var input = document.createElement("input");
			input.setAttribute("type", "checkbox");
			span.appendChild(input);
			cbxwrapper.appendChild(span);

			return cbxwrapper;
		},
		addCss: function(){
			var link = document.createElement("link");
			link.setAttribute("rel", "stylesheet");
			link.href = "http://localhost:8088/hostscripts/muhawo@163.com/cl.man.lv.css";
			document.head.appendChild(link);
		},
		buildToolbar: function(parentNode, topBottom){
			var div = document.createElement("div");
			div.id="clman-cbx-toolbar-"+topBottom;
			div.className = "clman-cbx-toolbar";
			var label1 = document.createElement("label");
			label1.setAttribute("for", "allselect-"+topBottom);
			label1.textContent = "全选";
			var input1 = document.createElement("input");
			input1.setAttribute("type", "checkbox");
			input1.id = "allselect-"+topBottom;
			bindSelectall(input1);

			var span = document.createElement("span");
			span.textContent = "下载选中";
			span.className = "clman-btn";
			div.appendChild(span);
			var spliter = document.createElement("span");
			spliter.className = "clman-spliter";
			spliter.textContent = "|";
			div.appendChild(spliter);
			div.appendChild(label1);
			div.appendChild(input1);
			parentNode.appendChild(div);
			function bindSelectall(ele){
				ele.addEventListener("click", function(event){
					var cbxes = document.querySelectorAll(".clman-cbx-wrapper input, .clman-cbx-toolbar input");
					for(var i = 0; i < cbxes.length; i++){
						cbxes[i].checked = event.target.checked;
					}
				})
			}
		},
		getUrlFromRespText: function(responseText, regName){
			var regs = {
				rmdown: />(http:\/\/www\.rmdown\.com.*?)</
			}
			var result = responseText.match(regs[regName]);
			return result? result[1]:"";
		},
		download: function(url){
			var that = this;
			var xmlHttp = new XMLHttpRequest();
			xmlHttp.open("GET",url,true);
			xmlHttp.onreadystatechange = function(){
				if(xmlHttp.readyState==4){
					if(xmlHttp.status==200){
						var downUrl = that.getUrlFromRespText(xmlHttp.responseText, "rmdown");
						var _downUrl = "http://www.rmdown.com/download.php";
						var ref = downUrl.split("=")[1];
						xmlHttp.open("POST", location.host+"?muhawo_downloadUrl="+_downUrl+"&ref="+ref+"&filename="+ref+".torrent", true);
						// xmlHttp.setRequestHeader('Content-Type', "cross-url:"+_downUrl);
						xmlHttp.onreadystatechange = function(){
							console.log(xmlHttp.status);
						}
						xmlHttp.send("ref="+ref);
					}
				}
			}
			xmlHttp.send(null);
		}
	}
	main.start();
})()